<?php

	/* description	: system configuration center
	 * developer	: BREEZEER
	 * release date	: 2022.10.31
	 * version		: 1.0.0
	 *
	 *
	 */
    namespace App\common;

	/* 配置中心版本切换标识 => 从当前选择的公司动态获取数据库和redis配置信息
	 *
	 *
	 *
	 *
	 *
	 *
	 *
	 */
	define('define_system_configuration_center_default'		,'1.0');
	# define('define_system_configuration_center_default'	,'2.0');
	// define('define_system_configuration_center'				,(@app()->request->current_company==''?define_system_configuration_center_default:app()->request->current_company));

    define('define_system_configuration_center'						,get_current_company());function get_current_company ()
    {
        # compatible php cli mode
        #
        #
        if(php_sapi_name() == "cli")
        {
            $current_company		= define_system_configuration_center_default;
        }
        else
        {
            # get current company type from client request header or uri address value
            #
            #
            #
            # modified by BREEZEER on 2023.02.21
            #
            #
            #
            $current_company		= request()->header("Com-Id")	/* 1. get value from header							*/;if($current_company==''){

            $current_company	= request()->get('com_id');		/* 2. get value from url address query parameter	*/
        }
        }

        #
        #
        #
        return in_array($current_company,['','undefined',66/* front end perhaps give me an undefined value of Com-Id then i will use default company configuration file */]) ? define_system_configuration_center_default:$current_company;
    }



    /* configuration center manager implements
     *
     *
     *
     *
     *
     *
     */
	class cConfigurationCenter
	{
		/* static singleton configuration object
		 *
		 */
		private static $m_g_configuration_center_detail_parameter;

		/* get configuration value or object self
		 *
		 */
		public function get($key=null)
		{
			return $key==null?self::$m_g_configuration_center_detail_parameter:self::$m_g_configuration_center_detail_parameter->$key;
		}
		public function __construct()
		{
			# system configuration file full absolute path
			#
			#
			$this->m_define_system_configuration_center																						= (__DIR__.'/system.configuration.center/'.define_system_configuration_center.'/system.configuration.center.php');

			# file exist ?
			#
			#
			if(file_exists($this->m_define_system_configuration_center)===true)
			{
			}
			else
			{
				die ('fatal error : company configuration file ['.define_system_configuration_center.'] information not exist');
			}

			# dynamic loading configuration center 
			#
			#
			if(self::$m_g_configuration_center_detail_parameter==null)
			{
				include_once($this->m_define_system_configuration_center);self::$m_g_configuration_center_detail_parameter					= $g_configuration_center_detail_parameter;
			}
		}

		/* 公司配置文件固定存放路径: 项目根目录/app/common/system.configuration.center
		 *																			|	
		 *																			|	
		 *																			+------ 1. 公司配置信息	
		 *																			|
		 *																			|
		 *																			+------ 2. 公司配置信息	
		 *																			|
		 *																			|
		 *																			+------ 3. 公司配置信息	
		 *																			|
		 *																			|
		 *																			+------ 4. 公司配置信息	
		 *																			|
		 *																			|
		 *																			+------ 5. 公司配置信息	
		 *																			|
		 *																			|
		 *																			+------ 6. 公司配置信息	
		 *																			|		
		 *																			|		.
		 *																			|		.
		 *																			|		.
		 *																			|		.
		 *																			|		.
		 *																			|		
		 *																			+------ N. 公司配置信息	
		 *
		 *
		 * tips :
		 *			1. 公司配置文件是一个非常重要的资源文件,包括且不限于如: mysql 连接字符串 第三方登录key 特殊的安全key 
		 *			2. 此函数返回所有公司配置文件包含的mysql连接字符串信息
		 *			
		 *			
		 *			
		 *			
		 *			
		 *			
		 *			
		 *			
		 */
		public function get_all_company_mysql_connection_string()
		{
			# get configuration file root folder
			#
			#
			$config_root_folder										= (__DIR__.'/system.configuration.center'	)	;
			$ar_dir													= glob($config_root_folder.'/*',GLOB_ONLYDIR)	;
			$ar_all_company_config									= []											;
			foreach($ar_dir as $one_dir)
			{
				# read one company mysql connection string
				#
				$one_company_config_file							= $one_dir.'/system.configuration.center.php'	;require($one_company_config_file);		

				# save configuration
				#
				$ar_all_company_config [basename($one_dir)]			= (object)		[
																						'mysql' => ($g_configuration_center_detail_parameter->mysql_connection_string_configuration)(),
																						'redis' => ($g_configuration_center_detail_parameter->redis_connection_string_configuration)(),
																					];
			}

			return $ar_all_company_config;
		}
	} 